package com.bjdd.jdy.ui.pay;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.DistanceUtil;
import com.bjdd.jdy.Constant;
import com.bjdd.jdy.baidu.LocationUtils;
import com.bjdd.jdy.entity.PayBean;
import com.bjdd.jdy.util.JumpUtil;
import com.bjdd.jdy.util.L;
import com.bjdd.jdy.util.RxSchedulers;
import com.bjdd.jdy.util.SpUtil;
import com.bjdd.jdy.util.T;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;

/**
 * Created by Administrator on 2017/5/9.
 */

public class PayPresenter extends  PayContract.Presenter {
    private double lat,lng;
    @Override
    public void onStart() {
        if (SpUtil.getUser() == null || SpUtil.getUser().equals("") || SpUtil.getUser().getCarrierId() == null) {
            JumpUtil.toLogin(context);
            return;
        }
        rxManager.on(Constant.EVENT_LOCATION_ONCE, msg -> locationBack((BDLocation) msg));
        rxManager.on(Constant.SIGN_ORDER_BACK,msg -> view.payBack());
        rxManager.on(Constant.PAY_SUCCESS,msg -> view.payBack());
    }
    @Override
    void getUrl(String orderNo) {
        String token = SpUtil.getToken();
        rxManager.add(model.cod(token,orderNo)
                .subscribe(result -> {
                    if (!result.isError()){
                        view.codSuccess(result.getData());
                    }else {
                        T.show(result.getMsg());
                    }
                },e -> {
                    T.show("获取二维码失败");
                }));
    }

    @Override
    void helpPay(String orderNo) {
        showLoadingDialog(false);
        String token = SpUtil.getToken();
        rxManager.add(model.helpPay(token,orderNo)
        .subscribe(result -> {
            hideLoadingDialog();
            if (!result.isError()){
                view.paySuccess(result.getData());
                //weiXinPay(result.getData());
            }else {
                T.show(result.getMsg());
            }
        },e -> {
            hideLoadingDialog();
            T.show("获取支付参数失败");
        }));
    }

    @Override
    boolean isRange(double lat, double lng, double lat1, double lng1) {
        LatLng p1 = new LatLng(lat1,lng1);
        LatLng p2 = new LatLng(lat,lng);
        double distance = DistanceUtil.getDistance(p1,p2);
        //T.show("距离："+distance);
        L.d("签收距离："+distance);
        if (distance > 1000)
            return false;
        return true;
    }

    @Override
    void isCanSign(double lat, double lng) {
        showLoadingDialog(false);
        this.lat = lat;
        this.lng = lng;
        LocationUtils.getInstance(context).startOnce();
    }

    private void locationBack(BDLocation location){
        hideLoadingDialog();
        if (isRange(lat,lng,location.getLatitude(),location.getLongitude())){
            view.canSign();
        }else {
            view.canNotSign();
        }
    }

    private void weiXinPay(PayBean payBean){
        new Thread(new Runnable() {
            @Override
            public void run() {
                IWXAPI api = WXAPIFactory.createWXAPI(context,null);
                api.registerApp(payBean.getAppid());
                PayReq request = new PayReq();
                request.appId = payBean.getAppid();
                request.partnerId = payBean.getPartnerid();
                request.prepayId = payBean.getPrepayid();
                request.packageValue = payBean.getPackageX();
                request.nonceStr = payBean.getNoncestr();
                request.timeStamp = payBean.getTimestamp();
                request.sign = payBean.getSign();
                api.sendReq(request);
            }
        }).start();
    }
}
